//#include <QtSvg>
#include "mainwindow.h"
#include <stdio.h>

#define FONTW 10

Calendar::Calendar(QWidget *parent) : QCalendarWidget(parent)
{
	exercises = new QMap<QString, QString>();
	setVerticalHeaderFormat(QCalendarWidget::NoVerticalHeader);

	db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName(QDir::homePath() + "/.dataKPant/dataKPant.db");
	if (db.open()) {
		QSqlQuery query("SELECT Date, Length FROM exercises");
        	while (query.next()) {
	        	QString dateField = query.value(0).toString();
			QString lengthField = query.value(1).toString();
	        	exercises->insert(dateField, lengthField);
		}
        }
	connect(this, SIGNAL(selectionChanged()), this, SLOT(open()));
}

void Calendar::open()
{
	QDate date = selectedDate();
	if (db.open()) {
		QString q = "SELECT Filename FROM exercises WHERE Date = '" +
			date.toString("yyyyMMdd") + "'";
		QSqlQuery query(q);
		if (query.next()) {
	        	QString nameFile = query.value(0).toString();
			QString pathFile = QDir::homePath() + "/.dataKPant/" + nameFile;
			qDebug() << "Opening... " + pathFile;
			QFile file(pathFile);
			DataHRM* datafromcal = new DataHRM(file);
			//TODO: switch to GraphWid and show the graphs of dataformcal 
		}
        }
}

void Calendar::paintCell ( QPainter * painter, const QRect & rect, const QDate & date ) const
{	
	QFont font = QApplication::font();
	font.setPointSize(rect.width() / FONTW);
	painter->setFont(font);
	QString dateField = date.toString("yyyyMMdd");
	if (exercises->contains(dateField)) {
		QString lengthField = exercises->value(dateField);		

		/* VERSIONE SVG */
		/*
		QSvgRenderer *svgr = new QSvgRenderer(QString(":/images/calicon.svg"));
		QSize size = svgr->defaultSize();
		double fact = size.width() / rect.width();
		QRectF newRect(rect.left(), rect.top(), rect.width(),
			 	size.height() / fact);
		svgr->render (painter, newRect);
		*/

		/* VERSIONE PNG */
		QPixmap icon(":/images/calicon.png");
		QPoint posIcon(rect.left() + (rect.width() - icon.width()) / 2, rect.top());
		painter->drawPixmap(posIcon, icon);
		QRect rectText(rect.left(), rect.top() + icon.height(),
			rect.width(), rect.height() - icon.height());
		
		painter->setPen(Qt::blue);
		painter->drawText(rectText, Qt::AlignCenter, lengthField);
		
	}
	else {	
		if (date == QDate::currentDate())
		{
			font.setWeight(QFont::Bold);
			painter->setFont(font);
			painter->setPen(Qt::green);
			
		}
		else if (date.month() == monthShown()) {
			font.setWeight(QFont::Normal);
			painter->setFont(font);
			painter->setPen(Qt::black);

		}
		else {
			font.setWeight(QFont::Light);
			painter->setFont(font);
			painter->setPen(Qt::gray);			
		}
		painter->drawText(rect, Qt::AlignCenter, QString().setNum(date.day()));
		//QCalendarWidget::paintCell(painter, rect, date);
	}
}

void Calendar::resizeEvent(QResizeEvent * /* event */) {
	QTextCharFormat headerFormat;
	QFont font = QApplication::font();
	font.setPointSize((width() / 7) / FONTW);
	headerFormat.setFont(font);
	setHeaderTextFormat(headerFormat);
}
